package com.wzcl.app.config;

import com.github.wxpay.sdk.WXPayConfig;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Log4j2
@Data
@Component
@PropertySource({"classpath:application-config.properties"})
public class MyWxPayConfig implements WXPayConfig {

    @Value("${wxPay.appID}")
    private String appID;
    @Value("${wxPay.mchID}")
    private String mchID;
    @Value("${wxPay.key}")
    private String key;
    @Value("${wxPay.sandboxKey}")
    private String sandboxKey;
    @Value("${wxPay.notifyUrl}")
    private String notifyUrl;
    @Value("${wxPay.refund_notifyUrl}")
    private String refundNotifyUrl;
    //@Value("${wxPay.certPath}")
    //TODO 线上修改
    private String certPath = "D:\\证书\\apiclient_cert.p12";
    //private String certPath = "wxPay.certPath = /usr/local/soft/cert/apiclient_cert.p12";
    @Value("${wxPay.useSandbox}")
    private String useSandbox;

    @Value("${wxPay.spbillCreateIp}")
    private String spbillCreateIp;

    @Value("${wxPay.pcNativeNotify}")
    private String pcNativeNotify;



    public String getAppID() {
        return appID;
    }

    @Override
    public String getMchID() {
        return mchID;
    }


    @Override
    public String getKey() {
        if (Boolean.valueOf(useSandbox)) {
            return sandboxKey;
        }
        return key;
    }

    @SneakyThrows
    @Override
    public InputStream getCertStream() {
        File certFile = new File(certPath);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(certFile);
        } catch (FileNotFoundException e) {
            log.error("cert file not found, path={}, exception is:{}", certPath, e);
        }
        return inputStream;
    }

    @Override
    //HTTP(S) 连接超时时间，单位毫秒
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    //HTTP(S) 读数据超时时间，单位毫秒
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}
